import cv2 as cv
import numpy as ny

'''
    移动鼠标左键点击图像任意 区域、能动态显示该区域的BGR颜色值。
'''
window_name = 'Image Color Check '
file_name = '../image/color.png'
src_img = cv.imread(file_name)

def mouse_callback(event, x, y, flag, param):
    src_img = cv.imread(file_name)
    if event == cv.EVENT_LBUTTONDOWN:
        text = str(src_img[y, x])
        h, w = src_img[:2]
        if x > 200:
            x = x-100
        cv.putText(src_img, text, (x, y), cv.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 1,
                   cv.LINE_4, 0)
        cv.imshow(window_name, src_img)
        pass
    elif event == cv.EVENT_MOUSEMOVE:
        print('x = %d y = %d' % (x, y))


def color_check():
    cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
    cv.setMouseCallback(window_name, mouse_callback)
    cv.imshow(window_name, src_img)
    cv.waitKey(0)
    pass


if __name__ == '__main__':
    color_check()
